package com.onlineMIS.action.headQ.ipad;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.record.PageBreakRecord.Break;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import net.sf.json.JSONObject;

import com.onlineMIS.ORM.DAO.Response;
import com.onlineMIS.ORM.DAO.headQ.barCodeGentor.ProductBarcodeDaoImpl;
import com.onlineMIS.ORM.DAO.headQ.inventory.InventoryService;
import com.onlineMIS.ORM.DAO.headQ.inventoryFlow.HeadQInventoryStoreDAOImpl;
import com.onlineMIS.ORM.DAO.headQ.ipad.IpadService;
import com.onlineMIS.ORM.entity.headQ.SQLServer.ClientsMS;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.Brand;
import com.onlineMIS.ORM.entity.headQ.barcodeGentor.ProductBarcode;
import com.onlineMIS.ORM.entity.headQ.custMgmt.HeadQCust;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrder;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadQInventoryStore;
import com.onlineMIS.ORM.entity.headQ.inventoryFlow.HeadqInventoryFlowOrder;
import com.onlineMIS.ORM.entity.headQ.user.UserInfor;
import com.onlineMIS.common.Common_util;
import com.onlineMIS.common.SessionInfo;
import com.onlineMIS.common.loggerLocal;

import com.opensymphony.xwork2.ActionContext;

@Controller
public class IpadJSONAction extends IpadAction {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1155815720822402087L;
	
	
	@Autowired
	protected InventoryService inventoryService;
	@Autowired
	private HeadQInventoryStoreDAOImpl headQInventoryStoreDAOImpl;
	@Autowired
	private ProductBarcodeDaoImpl productBarcodeDaoImpl;
	

	public String searchClientByPY(){
		loggerLocal.info("IpadJSONAction-searchClientByPY" );
		List<HeadQCust> clients = ipadService.getClients(formBean.getPinyin());
		if (clients == null)
			clients = new ArrayList<HeadQCust>();

		Response response = new Response();
		response.setReturnValue(clients);
		
	
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	/**
	 * 盘点用的功能,查询信息
	 * 可以用productCode 查询， 也可以用条码查询
	 * @return
	 */
	public String searchProductByProductCode() {
		loggerLocal.info("IpadJSONAction-searchProductByProductCode" );
		Response response = new Response();
				
		try{
			response = ipadService.searchByProductCode(formBean.getBarcode(),formBean.getProductCode(),formBean.getStoreId(),formBean.getStoreType(), null, null);

		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e){
			response.setFail(e.getMessage());
			loggerLocal.error(e);
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String chooseCust(){
		loggerLocal.info("IpadJSONAction-chooseCust" );
		int clientId = formBean.getClientId();
		
		Response response = new Response();
		response = ipadService.chooseCustById(clientId);
		
		
		
		if (response.isSuccess()){
			HeadQCust clientsMS = (HeadQCust)response.getReturnValue();
			
			//判断用户是否是重新选择客户
			Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
			if (orderIdObj != null) {
			  response = ipadService.changeCustForOrder((Integer)orderIdObj, clientId);
			  if (response.isSuccess()) {
				  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, clientsMS.getName() +" " + clientsMS.getArea());
				  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, clientId);
				  response.setSuccess("切换客户成功");
			  } else {
				  response.setFail("无法找到这个客户" + clientId);
			  }
			} else {
			  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME, clientsMS.getName() +" " + clientsMS.getArea());
			  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID, clientId);
			  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_MODE, IpadService.ORDER_MODE_HAND);
			}
		} else 
			response.setFail("无法找到这个客户" + clientId);
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	/**
	 * 客户订货使用
	 * @return
	 */
//	public String searchByProductCode(){
//		loggerLocal.info("IpadJSONAction-searchByProductCode" );
//		Response response = new Response();
//		
//		Object clientIdObj = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_CLIENT_ID);
//		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
//		
//		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
//		
//		Integer clientId = null;
//		Integer orderId = null;
//		
//		if (clientIdObj != null)
//			clientId = (Integer)clientIdObj;
//		
//		if (orderIdObj != null)
//			orderId =  (Integer)orderIdObj;
//		
//		response = ipadService.searchByProductCode(formBean.getProductCode(),formBean.getStoreId(), clientId, orderId);
//		
//		try{
//		    jsonObject = JSONObject.fromObject(response);
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//		
//		return SUCCESS;
//	}
	
	/**
	 * 公司盘点使用功能
	 * @return
	 */
	public String orderByBarcode4Pandian(){
		loggerLocal.info("IpadJSONAction-orderByBarcode4Pandian :"  + formBean.getOrderMode());
        Response response = new Response();
		
		Object clientIdObj = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_CLIENT_ID);
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		Object storeIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_STORE_ID);
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		//app 端口默认的order mode
        int orderMode = super.getOrderMode();
        
        //用户选择的order mode
        int orderModeForm = formBean.getOrderMode();
        boolean isCheck = true;
        String errorMsg = "";
        if (storeIdObj ==null) {
        	//校验storeId
        	HeadQInventoryStore store = headQInventoryStoreDAOImpl.get(formBean.getStoreId(), true);
        	if (store !=null) {
        		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_STORE_ID, formBean.getStoreId());
			}       	 
		}else {
			//校验库房
			if (storeIdObj != formBean.getStoreId()) {
				HeadQInventoryStore store = headQInventoryStoreDAOImpl.get((int)storeIdObj, true);
				errorMsg += "当前盘点库房为："+store.getName() + ",请选择正确的盘点库房";
				isCheck = false;
			}else {
				//校验条码
				HeadQInventoryStore store = headQInventoryStoreDAOImpl.get(formBean.getStoreId(), true);
				if (StringUtils.isNotBlank(formBean.getBarcode())) {
					ProductBarcode barcode = productBarcodeDaoImpl.getByProductBarcode(formBean.getBarcode());
					if (store !=null && barcode != null) {
						int belongTo = store.getBelongTo();
						int productBelongTo = barcode.getProduct().getBrand().getBelong_To();
						if (belongTo !=productBelongTo) {	
							errorMsg += "该货品并不属于："+ store.belongToMap.get(store.getBelongTo())+"货品,所以无法选择："+store.getName()+"仓库";
							isCheck = false;
						}
					}
				}
			}			
		}
        
        //如果客户已经设置了订单模式，就是用客户的订单模式
        if (orderModeForm != 0)
        	orderMode = orderModeForm;
		
		if (clientIdObj == null){
			response.setFail("请先输入客户后再选货");
		}else if (!isCheck) {
			response.setFail(errorMsg);
		} else {
			try {
		       response = ipadService.orderProduct4Pandian(clientIdObj, orderIdObj, formBean.getBarcode(), formBean.getStoreId(), formBean.getQuantity(), loginUser, orderMode);
		       
		       if (response.isSuccess()){
		    	   Object returnValue = response.getReturnValue();
		    	   if (returnValue != null){
		    		   try {
		    			  Map<String, Integer> result = (Map)returnValue;
		    			  Integer orderId = result.get("orderId");
		    		      ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
		    		   } catch (Exception e2){
		   				response.setFail(e2.getMessage());
						loggerLocal.error(e2);
		    		   }
		    	   }
		       }
			} catch (Exception e){
				response.setFail(e.getMessage());
				loggerLocal.error(e);
			}
		}
		
		try{
			
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	/**
	 * 公司盘点使用功能
	 * @return
	 */
	public String orderProduct4Pandian(){
		loggerLocal.info("IpadJSONAction.orderProduct4Pandian");
		Response response = new Response();
		int orderMode = super.getOrderMode();
        //用户选择的order mode
        int orderModeForm = formBean.getOrderMode();
        if (orderModeForm != 0)
        	orderMode = orderModeForm;
        
		Object clientIdObj = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_CLIENT_ID);
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		Object storeIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_STORE_ID);
		
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		if (clientIdObj == null){
			response.setFail("请先输入客户后再选货");
		} else {
			try {
				int storeId = Integer.valueOf(String.valueOf(storeIdObj));
		       response = ipadService.headqOrderProduct(clientIdObj, orderIdObj,storeId, formBean.getPbId(), formBean.getQuantity(), loginUser, orderMode);
		       
		       if (response.isSuccess()){
		    	   Object returnValue = response.getReturnValue();
		    	   if (returnValue != null){
		    		   try {
		    			  Map<String, Integer> result = (Map)returnValue;
		    			  Integer orderId = result.get("orderId");
		    		      ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
		    		   } catch (Exception e2){
		   				response.setFail(e2.getMessage());
						loggerLocal.error(e2);
		    		   }
		    	   }
		       }
			} catch (Exception e){
				response.setFail(e.getMessage());
				loggerLocal.error(e);
			}
		}
		
		try{
			
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String orderByBarcode(){
		loggerLocal.info("IpadJSONAction-orderByBarcode :"  + formBean.getOrderMode());
        Response response = new Response();
		
		Object clientIdObj = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_CLIENT_ID);
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		//app 端口默认的order mode
        int orderMode = super.getOrderMode();
        
        //用户选择的order mode
        int orderModeForm = formBean.getOrderMode();
        
        //如果客户已经设置了订单模式，就是用客户的订单模式
        if (orderModeForm != 0)
        	orderMode = orderModeForm;
		
		if (clientIdObj == null){
			response.setFail("请先输入客户后再选货");
		} else {
			try {
		       response = ipadService.orderProduct(clientIdObj, orderIdObj, formBean.getBarcode(), formBean.getStoreId(), formBean.getQuantity(), loginUser, orderMode);
		       
		       if (response.isSuccess()){
		    	   Object returnValue = response.getReturnValue();
		    	   if (returnValue != null){
		    		   try {
		    			  Map<String, Integer> result = (Map)returnValue;
		    			  Integer orderId = result.get("orderId");
		    		      ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
		    		   } catch (Exception e2){
		   				response.setFail(e2.getMessage());
						loggerLocal.error(e2);
		    		   }
		    	   }
		       }
			} catch (Exception e){
				response.setFail(e.getMessage());
				loggerLocal.error(e);
			}
		}
		
		try{
			
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String orderProduct(){
		loggerLocal.info("IpadJSONAction.orderProduct");
		Response response = new Response();
		int orderMode = super.getOrderMode();
		Object clientIdObj = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_CLIENT_ID);
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		if (clientIdObj == null){
			response.setFail("请先输入客户后再选货");
		} else {
			try {
		       response = ipadService.orderProduct(clientIdObj, orderIdObj, formBean.getPbId(), formBean.getQuantity(), loginUser, orderMode);
		       
		       if (response.isSuccess()){
		    	   Object returnValue = response.getReturnValue();
		    	   if (returnValue != null){
		    		   try {
		    			  Map<String, Integer> result = (Map)returnValue;
		    			  Integer orderId = result.get("orderId");
		    		      ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID, orderId);
		    		   } catch (Exception e2){
		   				response.setFail(e2.getMessage());
						loggerLocal.error(e2);
		    		   }
		    	   }
		       }
			} catch (Exception e){
				response.setFail(e.getMessage());
				loggerLocal.error(e);
			}
		}
		
		try{
			
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String clearOrders(){
		loggerLocal.info("IpadJSONAction.clearOrders");
		Response response = new Response();
		
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID,null);
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,null);
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,null);
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	/**
	 * PDA 提交单据
	 * @return
	 */
	public String submitOrder(){
		loggerLocal.info("IpadJSONAction.submitOrder");
		Response response = new Response();
		
		int orderId = formBean.getOrderId();
		
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		if (orderIdObj == null && orderId == 0) {
			response.setFail("当前订单为空");
		} else {
			try {
		       response = ipadService.submitOrder(orderId,orderIdObj, loginUser, formBean.getComment());
		       
		       if (response.isSuccess()){
		   		  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID,null);
				  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,null);
				  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,null); 
		       }
			} catch (Exception e){
				response.setFail(e.getMessage());
				loggerLocal.error(e);
			}
		}
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 盘点系统提交单据
	 * @return
	 */
	public String submitHeadqInventoryOrder(){
		loggerLocal.info("IpadJSONAction.submitOrder");
		Response response = new Response();
		
		Object orderIdObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		if (orderIdObj == null ) {
			response.setFail("当前订单为空");
		} else {
			try {
		       response = ipadService.submitHeadqInventoryOrder((int)orderIdObj, formBean.getComment());
		       
		       if (response.isSuccess()){
		   		  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID,null);
				  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,null);
				  ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,null); 
		       }
			} catch (Exception e){
				response.setFail(e.getMessage());
				loggerLocal.error(e);
			}
		}
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	/**
	 * 打开现有的单据
	 * @return
	 */
	public String editOrder4Pandian(){
		loggerLocal.info("IpadJSONAction.editOrder4Pandian");
		Response response = new Response();
		
		int orderId = formBean.getOrderId();
		
		response = ipadService.getOrderHeadInfo4Pandian(orderId);
		
		HeadqInventoryFlowOrder order = (HeadqInventoryFlowOrder)response.getReturnValue();
		
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,order.getId());
 		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_STORE_ID,order.getStoreId());
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,order.getStoreName()); ;
		
		response.setReturnValue("");
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;

	}
	
	/**
	 * 打开现有的单据
	 * @return
	 */
	public String editOrder(){
		loggerLocal.info("IpadJSONAction.editOrder");
		Response response = new Response();
		
		int orderId = formBean.getOrderId();
		
		response = ipadService.getOrderHeadInfo(orderId);
		
		InventoryOrder order = (InventoryOrder)response.getReturnValue();
		
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,order.getOrder_ID());
 		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID,order.getClient_id());
		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,order.getClient_name());
		
		Object orderModeObj =  ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_MODE);
		if (orderModeObj == null)
		       ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_MODE,IpadService.ORDER_MODE_HAND);
		
		response.setReturnValue("");
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;

	}
	

	
	public String deleteOrder(){
		loggerLocal.info("IpadJSONAction.deleteOrder");
		Response response = new Response();
		
		int orderId = formBean.getOrderId();
		
		response = ipadService.deleteOrder(orderId);
		
		if (response.isSuccess()){
		   Object orderIdObjSession = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		   if (orderIdObjSession != null && (Integer)orderIdObjSession == orderId){
				ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,null);
		 		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID,null);
				ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,null); 
		   }
		}
 		
		response.setReturnValue("");
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String deleteOrder4Pandian(){
		loggerLocal.info("IpadJSONAction.deleteOrder4Pandian");
		Response response = new Response();
		
		int orderId = formBean.getOrderId();
		
		response = ipadService.deleteOrder4Pandian(orderId);
		
		if (response.isSuccess()){
		   Object orderIdObjSession = ActionContext.getContext().getSession().get(IpadConf.HQ_SESSION_INFO_ORDER_ID);
		   if (orderIdObjSession != null && (Integer)orderIdObjSession == orderId){
				ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_ID,null);
		 		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CLIENT_ID,null);
		 		ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_ORDER_STORE_ID,null);
				ActionContext.getContext().getSession().put(IpadConf.HQ_SESSION_INFO_CUSTNAME,null); 
		   }
		}
 		
		response.setReturnValue("");
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	public String copyOrder(){
		loggerLocal.info("IpadJSONAction.copyOrder");
		UserInfor loginUser = (UserInfor)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER);
		
		
		Response response = new Response();
		
		int orderId = formBean.getOrderId();
		
		response = ipadService.copyOrder(orderId,loginUser);
		
		try{
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return SUCCESS;
	}
	
	
	/**
	 * 查询所有待配货单据
	 * @return
	 */
	public String listPdaOrders() {
		loggerLocal.info("IpadJSONAction-listPdaOrders" );		
		
		Integer custRange = (Integer)ActionContext.getContext().getSession().get(Common_util.IPAD_DISTRIBUTION_SELECT_RANGE);
		
		Response response = new Response();
		try {
		    response = inventoryService.getListPdaOrders(custRange);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 查询所有配货完成单据
	 * @return
	 */
	public String listPdaAlreadOrders() {
		loggerLocal.info("IpadJSONAction-listPdaAlreadOrders" );

		Integer custRange = (Integer)ActionContext.getContext().getSession().get(Common_util.IPAD_DISTRIBUTION_SELECT_RANGE);
		
		Response response = new Response();
		try {
		    response = inventoryService.getListPdaAlreadOrders(custRange);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	
	/**
	 * 根据条件查询总部配货单据
	 * @return
	 */
	public String searchInventoryOrders() {
		loggerLocal.info("IpadJSONAction-searchInventoryOrders" );
		Integer custRange = (Integer)ActionContext.getContext().getSession().get(Common_util.IPAD_DISTRIBUTION_SELECT_RANGE);
		
		Response response = new Response();
		try {
 		    response = inventoryService.searchPDAOrders4Peihuo(formBean, custRange);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	
	/**
	 * 查询总部客户信息
	 * @return
	 */
	public String listHeadqCust() {
		loggerLocal.info("IpadJSONAction-listHeadqCust" );

		
		Response response = new Response();
		try {
 		    response = inventoryService.listHeadqCust(formBean);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}

	
	
	
	
	
	/**
	 * 根据PDA单据号查询单据详情
	 * @return
	 */
	public String getPeihuoOrderById() {
		loggerLocal.info("IpadJSONAction-getPeihuoOrderById" );

		
		Response response = new Response();
		try {
		    response = inventoryService.getPeihuoOrderById(formBean.getOrderId());
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		if (response.getReturnCode() == Response.SUCCESS){
			
			jsonMap = (Map)response.getReturnValue();
			try {
				jsonObject = JSONObject.fromObject(jsonMap);
			} catch (Exception e){
				e.printStackTrace();
			}
		}
		return SUCCESS;
	}
	
	/**
	 * 编辑配货单据
	 * @return
	 */
	public String editInventoryById() {
		loggerLocal.info("IpadJSONAction-editInventoryById" );
		SessionInfo loginUserInfor = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		Response response = new Response();
		try {
		    response = inventoryService.updateInventoryOrderProduct(formBean.getOrderProductIndex(),formBean.getOrderId(),formBean.getPbId(),formBean.getQuantity(),formBean.getIsCheck(),loginUserInfor);
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		return SUCCESS;
	}
	
	
	/**
	 * 提交IPAD单据
	 * @return
	 */
	public String saveHeadIpadOrder(){
		loggerLocal.info("IpadJSONAction-saveHeadIpadOrder" );
		SessionInfo loginUserInfor = (SessionInfo)ActionContext.getContext().getSession().get(Common_util.LOGIN_USER_MOBILE);
		Response response = new Response();
		try {
		    response = inventoryService.updateInventoryOrder(formBean.getOrderId(),loginUserInfor);
		    jsonObject = JSONObject.fromObject(response);
		} catch (Exception e) {
			loggerLocal.error(e);
			response.setReturnCode(Response.FAIL);
		}
		return SUCCESS;
	}
	
}
